/**
 *=============================================================================
 *  @copyright (C)      2025CCUT_1024lab_Damocles
 *
 *  @file    template.c
 *  @brief   封装模块代码规范
 *
 *  @note    使用方法
 *
 *  @attention 注意事项
 *-----------------------------------------------------------------------------
 *  @version    @author    @date    @note
 *  V1.0.0      Cherno    250808    done
 *
 *==============================================================================
 */

/*==============================Includes======================================*/
#include <string.h>

#include "CRC8_CRC16.h"
#include "dvc_miniPC.h"
/*==============================PrivateMacros=================================*/
#define UINT8_TO_UINT16(uint8_ptr, offset)               \
	((uint16_t)(((const uint8_t *)(uint8_ptr))[offset] | \
	            (((const uint8_t *)(uint8_ptr))[(offset) + 1] << 8)))

#define UINT8_TO_UINT32(uint8_ptr, offset)                             \
	((uint32_t)(((const uint8_t *)(uint8_ptr))[offset] |               \
	            (((const uint8_t *)(uint8_ptr))[(offset) + 1] << 8) |  \
	            (((const uint8_t *)(uint8_ptr))[(offset) + 2] << 16) | \
	            (((const uint8_t *)(uint8_ptr))[(offset) + 3] << 24)))

#define BUFFER_TO_STRUCT(buffer, struct_ptr, size, offset)             \
	do {                                                               \
		if ((buffer) != NULL && (struct_ptr) != NULL) {                \
			memcpy((struct_ptr), (const uint8_t *)(buffer) + (offset), \
			       (size));                                            \
		}                                                              \
	} while (0)

#define STRUCT_TO_BUFFER(struct_ptr, buffer, size, offset)                \
	do {                                                                  \
		if ((buffer) != NULL && (struct_ptr) != NULL) {                   \
			memcpy((uint8_t *)(buffer) + (offset), (struct_ptr), (size)); \
		}                                                                 \
	} while (0)
/*==============================PrivateTypes==================================*/

/*==============================PrivateVariables==============================*/

/*==============================PrivateFunctionDeclarations===================*/

/*==============================FunctionPrototypes============================*/

/**
 * @brief : 描述该函数主要功能
 * @note : 注意事项
 *
 * @param : 参数名（与函数里的参数一致）  参数的描述
 * @return : 返回值的说明
 */
void miniPC::init(UART_HandleTypeDef *_huart)
{
	if (_huart->Instance == USART1) {
		uart_manager = &uart1_manager;
	} else if (_huart->Instance == USART2) {
		uart_manager = &uart2_manager;
	} else if (_huart->Instance == USART3) {
		uart_manager = &uart3_manager;
	} else if (_huart->Instance == UART4) {
		uart_manager = &uart4_manager;
	} else if (_huart->Instance == UART5) {
		uart_manager = &uart5_manager;
	} else if (_huart->Instance == USART6) {
		uart_manager = &uart6_manager;
	}
}

void miniPC::send_packet_data(lable_e _lable, vision_tx_data_t *_struct_data,
                              uint16_t _length)
{
	tx_frame.lable = _lable;
	tx_frame.length = _length;
	STRUCT_TO_BUFFER(_struct_data, tx_frame.data, sizeof(vision_tx_data_t), 0);

	STRUCT_TO_BUFFER(&tx_frame, tx_buffer, _length + 7, 0);

	append_CRC16_check_sum(tx_buffer,_length+7);

	uart_send_data(uart_manager->huart, tx_buffer, _length+7);
}

void miniPC::send_packet_data(lable_e _lable,
                              navigation_tx_data_t *_struct_data,
                              uint16_t _length)
{
}
void miniPC::send_packet_data(lable_e _lable, referee_tx_data_t *_struct_data,
                              uint16_t _length)
{
}
void miniPC::unpacking()
{
	if (uart_manager->rx_buffer[0] == 0xA5 &&
	    uart_manager->rx_buffer[1] == 0x55) {
		switch (uart_manager->rx_buffer[2]) {
			case (VERSION_LABLE): {
				uint16_t length = UINT8_TO_UINT16(uart_manager->rx_buffer, 3);
				if (verify_CRC16_check_sum(uart_manager->rx_buffer,
				                           length + 7)) {
					BUFFER_TO_STRUCT(uart_manager->rx_buffer, &vs_rx_data,
					                 length, 5);
				}

			} break;
			case (NAVIGATION_LABLE): {
				uint16_t length = UINT8_TO_UINT16(uart_manager->rx_buffer, 3);
				if (verify_CRC16_check_sum(uart_manager->rx_buffer,
				                           length + 7)) {
					BUFFER_TO_STRUCT(uart_manager->rx_buffer, &nvg_tx_data,
					                 length, 5);
				}
			} break;
		}
	} else {
		;
	}
}

void miniPC::UART_RxCallback(uint8_t *_rx_data) { unpacking(); }

/*========================COPYRIGHT(C) CCUT-DAMOCLES==========================*/